package za.co.dylanjaneke.airdownloader.model.vo
{
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.events.SQLEvent;
	
	import mx.collections.ArrayCollection;

	[Bindable]
	public class DownloadSiteVO implements DBVO
	{
		public var id:Number;
		public var url:String;
		
		public function DownloadSiteVO(url:String=null): void
		{
			this.url= url;
		}
		
		public function get createQuery():String
		{
			return "CREATE TABLE IF NOT EXISTS ad_dl_site (id INTEGER PRIMARY KEY AUTOINCREMENT, url TEXT);";
		}
		
		public static function valueOf(event:SQLEvent): ArrayCollection
		{
			var statement:SQLStatement= event.currentTarget as SQLStatement;
			var result:SQLResult= statement.getResult();
			if(result==null|| result.data==null)
				return null;
		
			var col:ArrayCollection= new ArrayCollection();
			
			for each(var row:Object in result.data)
			{
				var vo:DownloadSiteVO= new DownloadSiteVO(row.url);
				vo.id= row.id;
				col.addItem(vo);
			}
			
			return col;
		}
		
		public static function createInsert(vo:DownloadSiteVO, statement:SQLStatement): SQLStatement
		{
			statement.text= "INSERT INTO ad_dl_site (url) VALUES(:url)";
			statement.parameters[":url"]= vo.url;
			
			return statement;
		}
		
		public static function createDelete(vo:DownloadSiteVO, statement:SQLStatement): SQLStatement
		{
			statement.text ="DELETE FROM ad_dl_site WHERE url = :url";
			statement.parameters[":url"]= vo.url;
			
			return statement;
		}
	}
}